﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Administration;
using de.rwth.rz.campus.sputils.Security;

namespace de.rwth.rz.campus.Mailer
{
    /// <remarks>
    /// 
    /// Copyright 2010 RWTH Aachen, CAMPUS Labs
    /// 
    /// Licensed under the Apache License, Version 2.0 (the "License");
    /// you may not use this file except in compliance with the License.
    /// You may obtain a copy of the License at
    ///
    /// http://www.apache.org/licenses/LICENSE-2.0
    ///
    /// Unless required by applicable law or agreed to in writing, software
    /// distributed under the License is distributed on an "AS IS" BASIS,
    /// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    /// See the License for the specific language governing permissions and
    /// limitations under the License.
    /// 
    /// </remarks>

    class GlobalResourcesEventReceiver : SPFeatureReceiver
    {
        #region Constants

        const string JOB_TITLE = "Deploy Global Resources";
        const string JOB_NAME = "job-deploy-global-resources";

        #endregion

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite web = new SPSite(((SPSite)properties.Feature.Parent).ID))
                {
                    // Check for an exising instance of the job
                    foreach (SPJobDefinition job in web.WebApplication.JobDefinitions)
                    {
                        if (job.Name == JOB_NAME && job.WebApplication.Name == web.WebApplication.Name)
                            job.Delete();
                    }
                    // Create new job
                    DeployGlobalResources gsJob = new DeployGlobalResources(JOB_NAME, web.WebApplication, properties.Definition.DisplayName);
                    gsJob.Title = JOB_TITLE;
                    // Set up the job to run once
                    gsJob.Schedule = new SPOneTimeSchedule(DateTime.Now);
                    gsJob.Update();
                }
            });
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite web = new SPSite(((SPSite)properties.Feature.Parent).ID))
                {
                    // delete the job
                    foreach (SPJobDefinition job in web.WebApplication.JobDefinitions)
                    {
                        if (job.Name == JOB_NAME &&
                            job.WebApplication.Name == web.WebApplication.Name)
                        {
                            job.Delete();
                        }
                    }
                    // Possibly delete the files on retract??
                }
            });
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}
